package com.beyondsoft.dtc.mobile.automation.base;

import java.io.File;
import java.io.InputStream;
import java.util.Properties;

/**
 * Created by Robin on 1/28/2016.
 */
public class GlobalConfig {
	private static GlobalConfig instance = null;
	private static Properties dbProps = new Properties();

	private String workDir = null;
	private String backUpDir = null;

	private String apiCompatibilityReportUrl = null;
	private String apiCompatibilityReportLogUrl = null;
	private String apiCalabashReportUrl = null;
	private String apiLettuceReportUrl = null;
	private String apiBackupRestoreReportUrl = null;
	private String apiAutomationStatusReportUrl = null;

	private String storageCompatibilityUploadUrl = null;
	private String storageCalabashUploadUrl = null;
	private String storageBackupRestoreUploadUrl = null;

	private long startWaitTimeout;
	private long performanceDumpInterval;
	private long screenshotInterval;
	private long monkeyTestRepeatInterval;
	private long monkeyTestRepeatCount;
	private long monkeyTestPctTouch;
	private long monkeyTestPctMotion;
	private long monkeyTestPctAppSwitch;
	private long monkeyTestPctSysKeys;
	private long monkeyTestPctAnyEvent;

	private boolean debugLogEnabled = false;

	private GlobalConfig() {
		initialize();
	}

	public  static GlobalConfig getInstance() {
		if (instance == null) {
			instance = new GlobalConfig();
		}
		return instance;
	}

	private void initialize() {
		try {
			InputStream is = getClass().getResourceAsStream(
					GlobalConstants.CONFIG_FILE);// 找到配置文件
			dbProps.load(is);

			debugLogEnabled = Boolean.valueOf(dbProps
					.getProperty("DEBUG_LOG_ENABLED", "false").toLowerCase()
					.trim());

			workDir = dbProps.getProperty("WORK_DIR", "/apktest/").trim();
			if (!workDir.endsWith(File.separator)) {
				workDir += File.separator;
			}

			backUpDir = dbProps.getProperty("BACKUP_DIR", "/apktest/backup/")
					.trim();
			if (!backUpDir.endsWith(File.separator)) {
				backUpDir += File.separator;
			}

			// //首先使用从服务起获取的url
			// apiBaseUrl = BaseController.API_URL;
			// if (apiBaseUrl == null || apiBaseUrl.length() == 0) {
			// apiBaseUrl = dbProps.getProperty("API_URL",
			// "http://127.0.0.1:7100").trim();
			// String sysApiBaseUrl = System.getProperty("API_URL");
			// if (!StringUtil.isEmpty(sysApiBaseUrl)) {
			// apiBaseUrl = sysApiBaseUrl; //use ENV variable if it existed
			// }
			//
			// if (apiBaseUrl.endsWith("/")) {
			// apiBaseUrl = apiBaseUrl.substring(0, apiBaseUrl.length() - 1);
			// }
			// }

			apiCompatibilityReportUrl = dbProps.getProperty(
					"API_COMPATIBILITY_REPORT_URL", "").trim();
			apiCompatibilityReportLogUrl = dbProps.getProperty(
					"API_COMPATIBILITY_REPORT_LOG_URL", "").trim();
			apiCalabashReportUrl = dbProps.getProperty(
					"API_CALABASH_REPORT_URL", "").trim();
			apiLettuceReportUrl = dbProps.getProperty("API_LETTUCE_REPORT_URL",
					"").trim();
			apiBackupRestoreReportUrl = dbProps.getProperty(
					"API_BACKUP_RESTORE_REPORT_URL", "").trim();
			apiAutomationStatusReportUrl = dbProps.getProperty(
					"API_AUTOMATION_STATUS_REPORT_URL", "").trim();

			// //首先使用从服务起获取的url
			// storageBaseUrl = BaseController.STORAGE_URL;
			// if (storageBaseUrl == null || storageBaseUrl.length() == 0) {
			// storageBaseUrl = dbProps.getProperty("STORAGE_URL",
			// "http://127.0.0.1:7100").trim();
			// String sysStorageUrl = System.getProperty("STORAGE_URL");
			// if (!StringUtil.isEmpty(sysStorageUrl)) {
			// storageBaseUrl = sysStorageUrl; //use ENV variable if it existed
			// }
			//
			// if (storageBaseUrl.endsWith("/")) {
			// storageBaseUrl = storageBaseUrl.substring(0,
			// storageBaseUrl.length() - 1);
			// }
			// }

			storageCompatibilityUploadUrl = dbProps.getProperty(
					"STORAGE_COMPATIBILITY_UPLOAD_URL", "").trim();
			storageCalabashUploadUrl = dbProps.getProperty(
					"STORAGE_CALABASH_UPLOAD_URL", "").trim();
			storageBackupRestoreUploadUrl = dbProps.getProperty(
					"STORAGE_BACKUP_RESTORE_UPLOAD_URL", "").trim();

			startWaitTimeout = Long.valueOf(dbProps.getProperty(
					"START_WAIT_TIMEOUT", "30000").trim());
			performanceDumpInterval = Long
					.valueOf(dbProps
							.getProperty(
									"PERFORMANCE_DUMP_INTERVAL",
									String.valueOf(GlobalConstants.DEFAULT_PERFORMANCE_DUMP_INTERVAL))
							.trim());
			screenshotInterval = Long
					.valueOf(dbProps
							.getProperty(
									"SCREENSHOT_INTERVAL",
									String.valueOf(GlobalConstants.DEFAULT_SCREENSHOT_INTERVAL))
							.trim());
			monkeyTestRepeatInterval = Long
					.valueOf(dbProps
							.getProperty(
									"MONKEY_TEST_REPEAT_INTERVAL",
									String.valueOf(GlobalConstants.DEFAULT_MONKEY_TEST_REPEAT_INTERVAL))
							.trim());
			monkeyTestRepeatCount = Long
					.valueOf(dbProps
							.getProperty(
									"MONKEY_TEST_REPEAT_COUNT",
									String.valueOf(GlobalConstants.DEFAULT_MONKEY_TEST_REPEAT_COUNT))
							.trim());

			monkeyTestPctTouch = Long
					.valueOf(dbProps
							.getProperty(
									"MONKEY_TEST_PCT_TOUCH",
									String.valueOf(GlobalConstants.DEFAULT_MONKEY_TEST_PCT_TOUCH))
							.trim());
			monkeyTestPctMotion = Long
					.valueOf(dbProps
							.getProperty(
									"MONKEY_TEST_PCT_MOTION",
									String.valueOf(GlobalConstants.DEFAULT_MONKEY_TEST_PCT_MOTION))
							.trim());
			monkeyTestPctAppSwitch = Long
					.valueOf(dbProps
							.getProperty(
									"MONKEY_TEST_PCT_APPSWITCH",
									String.valueOf(GlobalConstants.DEFAULT_MONKEY_TEST_PCT_APPSWITCH))
							.trim());
			monkeyTestPctSysKeys = Long
					.valueOf(dbProps
							.getProperty(
									"MONKEY_TEST_PCT_SYSKEYS",
									String.valueOf(GlobalConstants.DEFAULT_MONKEY_TEST_PCT_SYSKEYS))
							.trim());
			monkeyTestPctAnyEvent = Long
					.valueOf(dbProps
							.getProperty(
									"MONKEY_TEST_PCT_ANYEVENT",
									String.valueOf(GlobalConstants.DEFAULT_MONKEY_TEST_PCT_ANYEVENT))
							.trim());
		} catch (Exception e) {
			e.printStackTrace();
		}
		// }
	}

	public String getWorkDir() {
		return workDir;
	}

	public String getBackUpDir() {
		return backUpDir;
	}

	public String getApiCompatibilityReportUrl() {
		return apiCompatibilityReportUrl;
	}

	public String getApiCalabashReportUrl() {
		return apiCalabashReportUrl;
	}

	public String getApiLettuceReportUrl() {
		return apiLettuceReportUrl;
	}

	public String getApiBackupRestoreReportUrl() {
		return apiBackupRestoreReportUrl;
	}

	public String getStorageCompatibilityUploadUrl() {
		return storageCompatibilityUploadUrl;
	}

	public String getStorageCalabashUploadUrl() {
		return storageCalabashUploadUrl;
	}

	public long getStartWaitTimeout() {
		return startWaitTimeout;
	}

	public long getPerformanceDumpInterval() {
		return performanceDumpInterval;
	}

	public long getScreenshotInterval() {
		return screenshotInterval;
	}

	public long getMonkeyTestRepeatInterval() {
		return monkeyTestRepeatInterval;
	}

	public long getMonkeyTestRepeatCount() {
		return monkeyTestRepeatCount;
	}

	public long getMonkeyTestPctTouch() {
		return monkeyTestPctTouch;
	}

	public long getMonkeyTestPctMotion() {
		return monkeyTestPctMotion;
	}

	public long getMonkeyTestPctAppSwitch() {
		return monkeyTestPctAppSwitch;
	}

	public long getMonkeyTestPctSysKeys() {
		return monkeyTestPctSysKeys;
	}

	public long getMonkeyTestPctAnyEvent() {
		return monkeyTestPctAnyEvent;
	}

	public boolean isDebugLogEnabled() {
		return debugLogEnabled;
	}

	public String getApiAutomationStatusReportUrl() {
		return apiAutomationStatusReportUrl;
	}

	public String getStorageBackupRestoreUploadUrl() {
		return storageBackupRestoreUploadUrl;
	}

	public String getApiCompatibilityReportLogUrl() {
		return apiCompatibilityReportLogUrl;
	}

	@Override
	public String toString() {
		return "GlobalConfig{" + "workDir='" + workDir + '\'' + ", backUpDir='"
				+ backUpDir + '\'' + ", apiCompatibilityReportUrl='"
				+ apiCompatibilityReportUrl + '\'' + ", apiCalabashReportUrl='"
				+ apiCalabashReportUrl + '\'' + ", apiLettuceReportUrl='"
				+ apiLettuceReportUrl + '\'' + ", apiBackupRestoreReportUrl='"
				+ apiBackupRestoreReportUrl + '\''
				+ ", apiAutomationStatusReportUrl='"
				+ apiAutomationStatusReportUrl + '\''
				+ ", storageCompatibilityUploadUrl='"
				+ storageCompatibilityUploadUrl + '\''
				+ ", storageCalabashUploadUrl='" + storageCalabashUploadUrl
				+ '\'' + ", storageBackupRestoreUploadUrl='"
				+ storageBackupRestoreUploadUrl + '\'' + ", startWaitTimeout="
				+ startWaitTimeout + ", performanceDumpInterval="
				+ performanceDumpInterval + ", screenshotInterval="
				+ screenshotInterval + ", monkeyTestRepeatInterval="
				+ monkeyTestRepeatInterval + ", monkeyTestRepeatCount="
				+ monkeyTestRepeatCount + ", monkeyTestPctTouch="
				+ monkeyTestPctTouch + ", monkeyTestPctMotion="
				+ monkeyTestPctMotion + ", monkeyTestPctAppSwitch="
				+ monkeyTestPctAppSwitch + ", monkeyTestPctSysKeys="
				+ monkeyTestPctSysKeys + ", monkeyTestPctAnyEvent="
				+ monkeyTestPctAnyEvent + ", debugLogEnabled="
				+ debugLogEnabled + '}';
	}
}
